﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class CaiGou_FuKuan_In : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             int BillID = 0;
             if (!this.IsPostBack)
             {
                 if (Request.QueryString["ID"] != null)
                 {
                     int.TryParse(Request.QueryString["ID"], out BillID);
                 }
                 if (BillID > 0)
                 {
                     SqlConnection Conn = new SqlConnection();
                     Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                     Conn.Open();
                     SqlCommand Cmd = new SqlCommand();
                     Cmd.Connection = Conn;
                     Cmd.CommandText = @"select 采购付款登记.ID,采购付款登记.付款单号,采购付款登记.付款日期,采购付款登记.合同ID,付款方式,采购付款登记.本次付款,采购付款登记.备注,应付金额.名称,应付金额.应付金额 from 采购付款登记 inner join  应付金额 on 应付金额.ID=采购付款登记.合同ID where 采购付款登记.ID=" + BillID;

                     Cmd.CommandType = CommandType.Text;
                     SqlDataReader Reader = Cmd.ExecuteReader();
                     if (Reader.Read())
                     {
                         this.ViewState["BillID"] = BillID;
                         this.GongHuoDanWei.DataBind();

                         this.FaPiaoHao.Text = Reader.GetString(1).Trim();
                         this.FaPiaoHao.ReadOnly = true;
                         this.RiQi.Text = Reader.GetDateTime(2).ToString("yyyy年M月d日");
                         this.RiQi.ReadOnly = true;
                         int i = Reader.GetInt32(3);
                         this.HeTongHao.Text = Reader.GetInt32(3).ToString();


                         this.FuKuan.Text = Reader.GetString(4);
                         this.BenCiFuKuan.Text = Reader.GetDecimal(5).ToString();                      

                         this.BeiZhu.Text = Reader.GetString(6);
                         this.GongHuoDanWei.Text = Reader.GetString(7);
                         this.YingFuKuan.Text = Reader.GetDecimal(8).ToString();   
                         Reader.Close();
                         SqlCommand cmd1 = new SqlCommand("select SUM(本次付款)as 付款合计 from 采购付款登记  where 合同ID="+i, Conn);
                         SqlDataReader sda1 = cmd1.ExecuteReader();
                         if (sda1.Read())
                         {
                             try
                             {

                                 Decimal 付款合计 = sda1.GetDecimal(0);
                                 this.YiFuJine.Text = 付款合计.ToString();//已付金额
                                 this.WeiFuJine.Text = (Decimal.Parse(this.YingFuKuan.Text) - 付款合计).ToString();//未付金额
                             }
                             catch (Exception ex)
                             {
                                 this.YiFuJine.Text = "0.00";
                                 this.WeiFuJine.Text = this.YingFuKuan.Text;
                             }
                         }
                         sda1.Close();
                     }
                     else
                     {
                         Reader.Close();
                     }

                     Conn.Close();
                     Reader.Dispose();
                     Cmd.Dispose();
                     Conn.Dispose();
                 }
                 else
                 {
                     this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                 }
             }
            
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            if (this.RiQi.Text == "")
            {
                Page.RegisterStartupScript("MSG", "<script language='javascript' for='window' event='onload'>alert('请选择付款日期！')</script>");
                return;
            }
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            SqlTransaction Tran = Conn.BeginTransaction();
            Cmd.Transaction = Tran;
            if (this.HeTongHao.Text != "0")
            {
                try
                {
                    if (this.ViewState["BillID"] == null)
                    {
                        this.ViewState["BillID"] = -1;
                    }

                    Cmd.CommandText = "CaiGou_FuKuan";
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.Add(new SqlParameter("@付款单号", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FaPiaoHao.Text));
                    Cmd.Parameters.Add(new SqlParameter("@付款日期", SqlDbType.Date, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(this.RiQi.Text)));
                    Cmd.Parameters.Add(new SqlParameter("@合同ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.HeTongHao.Text)));                   
                    Cmd.Parameters.Add(new SqlParameter("@付款方式", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FuKuan.Text));
                  
                    Cmd.Parameters.Add(new SqlParameter("@本次付款", SqlDbType.Money, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BenCiFuKuan.Text));                   
                    Cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BeiZhu.Text));
                    Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                    Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.ExecuteNonQuery();
                    this.Message.Text = "·保存成功";                   
                    Tran.Commit();

                }
                catch (Exception err)
                {
                    Tran.Rollback();
                    this.Message.Text = err.Message;
                }
                Cmd.Dispose();
                Tran.Dispose();
                Conn.Close();
                Conn.Dispose();
                if (this.Message.Text == "·保存成功")
                {
                    Response.Redirect("CaiGou_FuKuan_In_Search.aspx");
                }              
            }
        }

        protected void Button_Modify_Click(object sender, EventArgs e)
        {
            Response.Redirect("CaiGou_FuKuan_In_Search.aspx");
        }

        protected void HeTongHao_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.RiQi.Text = "";
            this.GongHuoDanWei.Text = "";     
            this.BeiZhu.Text = "";

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand cmd = new SqlCommand("select * from 应付金额 where 应付金额.合同编号='" + this.HeTongHao.SelectedItem.Text+ "'", Conn);
            SqlDataReader sda = cmd.ExecuteReader();            
            if (sda.Read())
            {
               
                String 供货单位 = sda.GetString(2);
                this.GongHuoDanWei.Text = 供货单位.ToString();
                Decimal yingfukuan=sda.GetDecimal(3);
                this.YingFuKuan.Text = yingfukuan.ToString();
            }
            sda.Close();

            SqlCommand cmd1 = new SqlCommand("select SUM(本次付款)as 付款合计 from 采购付款登记  where 合同ID="+this.HeTongHao.Text, Conn);
            SqlDataReader sda1 = cmd1.ExecuteReader();
            if (sda1.Read())
            {
                try
                {

                    Decimal 付款合计 = sda1.GetDecimal(0);
                    this.YiFuJine.Text=付款合计.ToString();//已付金额
                    this.WeiFuJine.Text = (Decimal.Parse(this.YingFuKuan.Text) - 付款合计).ToString();//未付金额
                }catch (Exception ex)
                {
                    this.YiFuJine.Text = "0.00";
                    this.WeiFuJine.Text = this.YingFuKuan.Text;
                }              
            }
            

            sda1.Close();
            Conn.Close();
        }
    }
}
